package cn.Alibaba.org.controller;


import cn.Alibaba.basic.exception.BusinessException;
import cn.Alibaba.basic.util.ExcelUtil;
import cn.Alibaba.basic.util.JsonResult;
import cn.Alibaba.basic.util.PageList;
import cn.Alibaba.org.domain.Shop;
import cn.Alibaba.org.domain.ShopLog;
import cn.Alibaba.org.dto.ShopDto;
import cn.Alibaba.org.query.ShopQuery;
import cn.Alibaba.org.service.IShopLogService;
import cn.Alibaba.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequestMapping("/shop")
@Api(value = "店铺的API",description="店铺的相关的CRUD功能")
@RestController
public class ShopController {

    @Autowired
    private IShopService shoptService;

    @Autowired
    private IShopLogService shopLogService;



    //商家入驻接口 //先写主体，再写细节
    @ApiOperation(value = "商家入驻",notes = "")
    @PostMapping("/settlement")
    public JsonResult settlement(@RequestBody Shop shop){

        try {

            shoptService.settlement(shop);

            return JsonResult.me();

        } catch (BusinessException e) {

            e.printStackTrace();
            return JsonResult.me().setMsg("入驻失败"+e.getMessage());

        }catch (Exception e) {

            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙,请稍后重试！");

        }


    }





    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询展示所有数据" )
    public List<Shop> findAll(){
        return shoptService.findAll();
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询数据" )
    public Shop findOne(@PathVariable("id") Long id){
        return shoptService.findOne(id);

    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除数据" )
    public JsonResult delete(@PathVariable("id") Long id){

        try {
            shoptService.del(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    @PatchMapping
    @ApiOperation(value = "通过ID删除数据" )
    public JsonResult PatchDel(@RequestBody List<Long> ids){

        try {
            shoptService.PatchDel( ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 添加修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "通过ID添加或者修改数据" )
    public JsonResult addOrUpdate(@RequestBody  Shop shop ){
        try {
            if (shop.getId()==null){
                shoptService.save(shop);
            }else {
                shoptService.update(shop);
            }

            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();

            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询数据" )
    public PageList<Shop> querPage(@RequestBody ShopQuery query){
        return shoptService.queryPage(query);

    }


    /**
     * 审核通过
     * @param log
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "审核通过" )
    public JsonResult pass(@RequestBody ShopLog log){

        try {
            shopLogService.pass(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("审核失败");
        }


    }


    /**
     * 驳回成功
     * @param log
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "审核驳回" )
    public JsonResult reject(@RequestBody ShopLog log){

        try {
            shopLogService.reject(log);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("驳回失败");
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出" )
    public void export(HttpServletResponse response){

        List<Shop> shops = shoptService.findAll();
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);


    }


    @PostMapping("/importExcel")
    @ApiOperation(value = "导出" )
    public void export(@RequestPart("file")MultipartFile file){

        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);


    }

    @GetMapping("/echarts")
    public List<ShopDto> getEcharts(){
        List<ShopDto> shopDtos =shoptService.getEcharts();
        return shopDtos;

    }




}
